package com.guidance.guidance.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.guidance.guidance.domain.Message;
import com.guidance.guidance.domain.User;
import com.guidance.guidance.repository.UserRepository;

@RestController
@RequestMapping("/change")
public class ChangeInfoController {

	@Autowired
	private UserRepository userRepository;
	
	/**
	 * 修改昵称API,客户端调用时需要传入uid,新的name
	 * @param msg
	 * @param user
	 * @return
	 */
	@RequestMapping("/changeName")
	public Message<User> changeName(Message<User> msg , User u) {
		User user = userRepository.findByUid(u.getUid());
		if(user != null) {
			user.setName(u.getName());
			userRepository.save(user);
			if(userRepository.save(user)!=null) {
				msg.setCode("200");
				msg.setMsg("修改成功");
				msg.setSuccess("true");
			}else {
				msg.setCode("404");
				msg.setMsg("修改失败");
				msg.setSuccess("false");
			}
		}else {
			msg.setCode("404");
			msg.setMsg("操作有误");
			msg.setSuccess("false");
		}
		return msg;
	}
	
	
	/**
	 * 修改手机号API,客户端调用需要传入两个参数，分别是uid,新的telephone
	 * @param msg
	 * @param u
	 * @return
	 */
	@RequestMapping("/changePhone")
	public Message<User> changePhone(Message<User> msg , User u) {
		User user = userRepository.findByUid(u.getUid());
		List<User> userList = userRepository.findAll();
		for(User uu : userList) {
			if (uu.getTelephone().equals(u.getTelephone())) {
				msg.setCode("404");
				msg.setMsg("该手机号已存在");
				msg.setSuccess("false");
				return msg;
			}
		}
		if(user != null) {
			user.setTelephone(u.getTelephone());
			userRepository.save(user);
			if(userRepository.save(user)!=null) {
				msg.setCode("200");
				msg.setMsg("修改成功");
				msg.setSuccess("true");
			}else {
				msg.setCode("404");
				msg.setMsg("修改失败");
				msg.setSuccess("false");
			}
		}else {
			msg.setCode("404");
			msg.setMsg("操作有误");
			msg.setSuccess("false");
		}
		return msg;
		
	}
	
	/**
	 * 判断该手机号是否注册过的API
	 * 传入参数只有一个telephone
	 */
	@RequestMapping("/isExitTelephone")
	public Message<User> isExitTelephone(Message<User> msg , User u) {
		User user = userRepository.findByTelephone(u.getTelephone());
		if(user == null) {
			msg.setCode("404");
			msg.setMsg("没有该用户");
			msg.setSuccess("false");
		}else {
			msg.setCode("200");
			msg.setMsg("该用户注册过");
			msg.setSuccess("true");
		}
		return msg;
	}
	
	
	
	/**
	 * 忘记密码API,客户端使用的时候要传入两个参数，分别是telephone和新的password
	 * 1.先判断该手机号是否已经注册过，注册过才添加新密码
	 * @param msg
	 * @param u
	 * @return
	 */
	@RequestMapping("/forgetPassword")
	public Message<User> forgetPassword(Message<User> msg, User u) {
		User user = userRepository.findByTelephone(u.getTelephone());
		user.setPassword(u.getPassword());
		userRepository.save(user);
		if(userRepository.save(user)!=null) {
			msg.setCode("200");
			msg.setMsg("修改成功");
			msg.setSuccess("true");
		}else {
			msg.setCode("404");
			msg.setMsg("修改失败");
			msg.setSuccess("false");
		}
		return msg;
	}
	
	/**
	 * 修改密码API
	 * 客户端使用时需要传入三个参数，分别是telephone、newPassword
	 * @param msg
	 * @param u
	 * @return
	 */
	@RequestMapping("/changePassword")
	public Message<User> changePassword(Message<User> msg ,User u) {
		User user = userRepository.findByTelephone(u.getTelephone());
		if(user != null) {
			user.setPassword(u.getPassword());
			userRepository.save(user);
			if(userRepository.save(user)!=null) {
				msg.setCode("200");
				msg.setMsg("修改成功");
				msg.setSuccess("true");
			}else {
				msg.setCode("404");
				msg.setMsg("修改失败");
				msg.setSuccess("false");
			}
		}else {
			msg.setCode("404");
			msg.setMsg("不是当前登录账号");
			msg.setSuccess("false");
		}
		return msg;
	}
	
	
	/**
	 * 上传头像，客户端使用需要传入一个参数，分别是file
	 * @param msg
	 * @param req
	 * @param multiReq
	 */
	
	@RequestMapping("/uploadHeadPhoto")
	public Message<User> uploadFile(Message<User> msg ,@RequestParam("avatar")MultipartFile file , HttpServletRequest request,User u) {
		System.out.println(request.getParameter("telephone"));
//		System.out.println(headPhoto+"   2   2  2");
		if(!file.isEmpty()) {
			User user = userRepository.findByTelephone(request.getParameter("telephone"));
			String saveFileName = file.getOriginalFilename();
//			File saveFile = new File(request.getSession().getServletContext().getRealPath("/upload")+saveFileName);
			long time = System.currentTimeMillis();
			File saveFile = new File("/usr/local/photo/"+time+saveFileName);
			if(!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			System.out.println(saveFile.getAbsolutePath());
			try {
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
				out.write(file.getBytes());
				out.flush();
				out.close();
				if(user != null) {
					user.setHeadPhoto("http://120.78.188.189:8080/GuidanceManagement/userPhoto/"+time+saveFileName);
					userRepository.save(user);
					msg.setCode("200");
					msg.setMsg("上传成功");
					msg.setSuccess("true");
					msg.setData(user);
				}else {
					msg.setCode("404");
					msg.setMsg("上传失败");
					msg.setSuccess("false");
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				msg.setCode("404");
				msg.setMsg("上传失败");
				msg.setSuccess("false");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				msg.setCode("404");
				msg.setMsg("上传失败");
				msg.setSuccess("false");
			}
		}else {
			msg.setCode("404");
			msg.setMsg("上传失败，因为文件为空");
			msg.setSuccess("false");
		}
		return msg;
	}
	
}
